package com.example.bank.service.result;

import cn.hutool.core.util.ObjectUtil;
import com.example.bank.dao.RecordDao;
import com.example.bank.dao.ResultDao;
import com.example.bank.pojo.record.Record;
import com.example.bank.pojo.record.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：Leeziqiang
 * @description：TODO
 * @date ：2022/1/18 14:35
 */
@Service
@Transactional
public class ResultServiceImpl implements ResultService{

    private static final Logger logger = LoggerFactory.getLogger(ResultServiceImpl.class);
    @Autowired
    private ResultDao resultDao;
    @Autowired
    private RecordDao recordDao;


    @Override
    public boolean saveResult(String vin, Result result) {
        // 首先判断数据库中是否存在
        Result dbResult = resultDao.findById(vin);
        // 若不存在则插入
        if (ObjectUtil.isNull(dbResult)) {
            int n = resultDao.addResult(vin, result.getCar_is_fire(), result.getCar_is_water()
            , result.getStructure_is_repair(), result.getAppearance_is_repair()
            , result.getKilomete_is_normal(), result.getImportant_parts_is_repair()
            , result.getLast_maintain_time(), result.getLast_repair_time()
            , result.getLast_to_shop_time(), result.getLast_kilometer());
            for (Record record : result.getContent()) {
                recordDao.addRecord(record.getMaintain_dateline(), record.getMaintain_type(),
                        record.getKilomete(), record.getContent(), record.getMeterial(), vin);
            }
            return true;
        }
        // 若存在则修改
        return false;
    }

    //LBVTZ010XLSW20121
    @Override
    public Result findById(String vin) {
        Result result = resultDao.findById(vin);
        if (ObjectUtil.isNull(result)) {
            return null;
        }
        List<Record> list = recordDao.findByResultId(vin);
        Record[] records = new Record[list.size()];
        for (int i = 0; i < list.size(); i++) {
            records[i] = list.get(i);
        }
        if (result != null) {
            result.setContent(records);
        }
        return result;
    }
}
